package com.itheima.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.project.constant.SuperConstant;
import com.itheima.project.mapper.ResourceMapper;
import com.itheima.project.mapper.RoleMapper;
import com.itheima.project.mapper.UserMapper;
import com.itheima.project.pojo.Resource;
import com.itheima.project.pojo.Role;
import com.itheima.project.pojo.User;
import com.itheima.project.service.IUserAdapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName UserAdapterServiceImpl.java
 * @Description 后台登陆用户适配器接口实现
 */
@Service
public class UserAdapterServiceImpl implements IUserAdapterService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private ResourceMapper resourceMapper;

    @Override
    public User findUserByMobileAndEnterpriseId(String mobil, Long enterpriseId) {
        QueryWrapper<User> queryWrapper =new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(User::getEnableFlag, SuperConstant.YES)
                .eq(User::getMobile, mobil)
                .eq(User::getEnterpriseId, enterpriseId);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public User findUserByUsernameAndEnterpriseId(String username,Long enterpriseId) {
        QueryWrapper<User> queryWrapper =new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(User::getEnableFlag, SuperConstant.YES)
                .eq(User::getUsername, username)
                .eq(User::getEnterpriseId, enterpriseId);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public User findUserByOpenIdAndEnterpriseId(String openId, Long enterpriseId) {
        QueryWrapper<User> queryWrapper =new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(User::getEnableFlag, SuperConstant.YES)
                .eq(User::getOpenId, openId)
                .eq(User::getEnterpriseId, enterpriseId);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public List<Role> findRoleByUserId(Long userId) {
        List<Role> list = roleMapper.findRoleByUserId(userId,SuperConstant.YES);
        return list;
    }

    @Override
    public List<Resource> findResourceByUserId(Long userId) {
        return resourceMapper.findResourceByUserId(SuperConstant.YES,userId);
    }

    @Override
    public User findUserByUserId(Long userId) {
        return userMapper.selectById(userId);
    }
}
